from rest_framework.response import Response
from rest_framework.views import APIView
from mylearn.models import LifaGoods,LifaUser
from mylearn.mylearnser import LifaGoodsSer,LifaUserSer

class LifadeLogin(APIView):
    def get(self,request):
        username=request.GET.get('username')
        password=request.GET.get('password')
        users=LifaUser.objects.filter(username=username,password=password).first()
        if users:
            return Response({'code':200,'message':"登录成功",'username':users.username,'uid':users.id})

#充值接口
class LifadeTop(APIView):
    def post(self,request):
        userprice=request.data['userprice']
        id=request.data['uid']
        print(id)
        print(userprice)

        user=LifaUser.objects.get(id=id)
        if user:
            user.userprice+=int(userprice)
            #如果充值钱 大于500元，就升级成为vip 用户
            if user.userprice>=500:
                user.vipuser=2
            user.save()
            print('相加之后的',user.userprice)
        return  Response({'message':'充值成功'})

class LifadeShow(APIView):
    def get(self,request):
        goods=LifaGoods.objects.all()
        goods_ser=LifaGoodsSer(goods,many=True)
        return ({'data':goods_ser.data})


